﻿/******************************************************/
// BOLCommon 事業運営クラス  
/******************************************************/

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using log4net;
using MAA.Data;
using MAA.Entity;

namespace MAA.Business
{
    /// <summary>
    /// Do bussiness logic refer to common & other processing
    /// </summary>
    public class BOLCommon
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string getPostStatus(short status)
        {
            string postStatus = "";
            switch (status)
            {
                case 1:
                    postStatus = Common.POST_STATUS_TRADING;
                    break;
                case 2:
                    postStatus = Common.POST_STATUS_COMPLETE;
                    break;
                case 3:
                    postStatus = Common.POST_STATUS_EXPIRED;
                    break;
                default:
                    break;
            }
            return postStatus;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string getOrderStatus(short status)
        {
            string orderStatus = "";
            switch (status)
            {
                case 1:
                    orderStatus = Common.ORDER_STATUS_PENDING;
                    break;
                case 2:
                    orderStatus = Common.ORDER_STATUS_ACCEPTED;
                    break;
                case 3:
                    orderStatus = Common.ORDER_STATUS_REJECTED;
                    break;
                default:
                    break;
            }
            return orderStatus;
        }

        /// <summary>
        /// Check type of file upload
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool checkFileImage(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="userName"></param>
        /// <param name="userEmail"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool getUserInfoForEmail(long userID, out string userName, out string userEmail, out string message)
        {
            message = "";

            DALUser dalUser = new DALUser();
            userName = "";
            userEmail = "";

            try
            {
                log.Info("getUserInfoForEmail");
                DataTable tblUser = dalUser.getAccInfo(userID);
                if (tblUser.Rows.Count > 0)
                {

                    int numRowUser = tblUser.Rows.Count;
                    for (int i = 0; i < numRowUser; ++i)
                    {
                        userName = Convert.ToString(tblUser.Rows[i]["Name"]);
                        userEmail = Convert.ToString(tblUser.Rows[i]["Email"]);
                    }
                }
                return true;
            }
            catch (SqlException ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="server"></param>
        /// <param name="userEmail"></param>
        /// <param name="userPass"></param>
        /// <param name="port"></param>
        /// <param name="displayName"></param>
        /// <param name="fromMail"></param>
        /// <param name="toMail"></param>
        /// <param name="ccMail"></param>
        /// <param name="bccMail"></param>
        /// <param name="subject"></param>
        /// <param name="content"></param>
        /// <param name="useSSL"></param>
        /// <param name="isHtmlContent"></param>
        /// <returns></returns>
        public static bool SendMailMulti(
            string server,
            string userEmail,
            string userPass,
            int port,
            string displayName,
            string fromMail,
            string toMail,
            string ccMail,
            string bccMail,
            string subject,
            string content,
            bool useSSL,
            bool isHtmlContent)
        {
            try
            {
                log.Info("Send mail multi");
                var from = new MailAddress(fromMail, displayName);
                var listTo = toMail.Split(',');
                var message = new MailMessage();
                message.From = from;
                for (int i = 0; i < listTo.Length; i++)
                {
                    message.To.Add(listTo[i]);
                }
                message.Subject = subject;
                message.Body = content;
                message.BodyEncoding = Encoding.UTF8;
                message.Priority = MailPriority.High;
                message.IsBodyHtml = isHtmlContent;
                SmtpClient client = new SmtpClient(server);
                client.Credentials = new NetworkCredential(userEmail, userPass);
                client.EnableSsl = useSSL;
                client.Port = port;
                client.Send(message);
                return true;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
                log.Error(ex.TargetSite);
                using (System.IO.StreamWriter file = new System.IO.StreamWriter("~/SendMailErr.txt", true))
                {
                    var str =
                        server + Environment.NewLine +
                        userEmail + Environment.NewLine +
                        userPass + Environment.NewLine +
                        port + Environment.NewLine +
                        displayName + Environment.NewLine +
                        fromMail + Environment.NewLine +
                        toMail + Environment.NewLine +
                        ccMail + Environment.NewLine +
                        bccMail + Environment.NewLine +
                        subject + Environment.NewLine +
                        content;
                    file.WriteLine(Environment.NewLine + ex.Message + Environment.NewLine + ex.Source + Environment.NewLine + ex.StackTrace + Environment.NewLine + str);
                }

                return false;
            }
        }
    }
}